﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TelephoneYiShou.XQL
{
    public partial class FrmBuyCount : Skin_Mac
    {
        public FrmBuyCount()
        {
            InitializeComponent();
        }
        public DataTable dt;
        int i, start;//i表示总行数，start表示当前位置
        int size = 6;//size表示一页显示的数量
        string str1 = "select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";
        private void FrmBuyCount_Load(object sender, EventArgs e)
        {
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //显示i到j的数据
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[OrderGoods]");//把数据库表中的i-j的记录数用来填充数据集
            dataOrderTable.DataSource = ds.Tables["[OrderGoods]"];
            ds = null;//清空数据集
        }
        //退出页面
        private void buttQuit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        //首页
        private void homepage_Click_1(object sender, EventArgs e)
        {
            show(0, size);//调用show函数
            start = 0;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //下一页
        private void NextPage_Click_1(object sender, EventArgs e)
        {
            if (start + 1 >= i / size)
            {
                MessageBoxEx.Show("已是尾页");
                show(start * size, size);
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //上一页
        private void UpPgae_Click_1(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页");
                show(0, size);
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //尾页
        private void TrailerPage_Click_1(object sender, EventArgs e)
        {

            show(i/size,i-(i/size)*size);
            start = i / size;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //订单数统计
        private void CountSum_Click_1(object sender, EventArgs e)
        {
            int count = dt.Rows.Count;
            MessageBoxEx.Show("订单数为：" + count);
        }
        //开始时间
        private void dateTimestart_ValueChanged(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text), Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
        }
        //结束时间
        private void dateTimeend_ValueChanged(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text), Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
        }
        //显示全部数据
        private void button1_Click(object sender, EventArgs e)
        {
            str1 = "select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID";
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //搜索
        private void buttselect_Click(object sender, EventArgs e)
        {
            start = 0;//初始化页数
            string gonshang = textgs.Text;
            if (string.IsNullOrEmpty(gonshang))
            {
                MessageBoxEx.Show("请输入要搜索的内容");
                return;
            }
            //按供应商查找
            if (comanxuanxiang.Text== "供应商")
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and s.SupplierName like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按商品查找
            if (comanxuanxiang.Text == "商品名称")
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and c.CommodityName like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按支付方式
            if (comanxuanxiang.Text == "支付方式")
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and Paytype like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }

        }
        private void dateTimestart_ValueChanged_1(object sender, EventArgs e)
        {

        }
        //开始时间
        private void dateTimestart_ValueChanged_2(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text), Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
        }
        //结束时间
        private void dateTimeend_ValueChanged_1(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text), Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
        }

        //金额，售出手机台数统计
        private void buttmoney_Click_1(object sender, EventArgs e)
        {
            int sum = 0;
            double moneysum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)//循环所有列数
            {
                sum += int.Parse(dt.Rows[i]["Getnumber"].ToString());//累加所有数量
                moneysum += double.Parse(dt.Rows[i]["Moneysum"].ToString());//累加金额
            }
            MessageBoxEx.Show("一共买入手机" + sum + "台\n" + "支出金额为" + moneysum + "元");
        }
    }
}
